package com.eshop.tools.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import java.util.List;
import com.eshop.tools.service.dto.LocalStorageDto;
import com.eshop.tools.service.dto.LocalStorageQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.tools.service.LocalStorageService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "文件管理" })
@RestController
@RequestMapping({ "/api/localStorage" })
public class LocalStorageController
{
    private final LocalStorageService localStorageService;
    private final IGenerator generator;
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','storage:list')")
    public void download(final HttpServletResponse response, final LocalStorageQueryCriteria criteria) throws IOException {
        this.localStorageService.download(this.generator.convert((List)this.localStorageService.queryAll(criteria), (Class)LocalStorageDto.class), response);
    }
    
    @GetMapping
    @Log("查询文件")
    @ApiOperation("查询文件")
    @PreAuthorize("@el.check('admin','storage:list')")
    public ResponseEntity<Object> getLocalStorages(final LocalStorageQueryCriteria criteria, final Pageable pageable) {
        return (ResponseEntity<Object>)new ResponseEntity((Object)this.localStorageService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @PostMapping
    @Log("新增文件")
    @ApiOperation("新增文件")
    @PreAuthorize("@el.check('admin','storage:add')")
    public ResponseEntity<Object> create(@RequestParam final String name, @RequestParam("file") final MultipartFile file) {
        return (ResponseEntity<Object>)new ResponseEntity((Object)this.localStorageService.create(name, file), HttpStatus.CREATED);
    }
    
    @PutMapping
    @Log("修改文件")
    @ApiOperation("修改文件")
    @PreAuthorize("@el.check('admin','storage:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final LocalStorageDto resources) {
        this.localStorageService.updateLocalStorage(resources);
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @Log("删除文件")
    @ApiOperation("删除文件")
    @PreAuthorize("@el.check('admin','storage:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Long[] ids) {
        Arrays.asList(ids).forEach(id -> this.localStorageService.removeById((Serializable)id));
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    public LocalStorageController(final LocalStorageService localStorageService, final IGenerator generator) {
        this.localStorageService = localStorageService;
        this.generator = generator;
    }
}
